// pages/myinfo/myMoney/recharge/recharge.js
var app = getApp();
const md5 = require('../../../../utils/md5.js');
const http = require('../../../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      msg:[],
      msgIndex:0,
      rechagePrice:'',
      rechargeAmount:'',
      formId:''
     
  },
  changeColor(e) {//充值多少钱
    console.log(e);
    let _this = this;
    let index = e.currentTarget.dataset.index;
      var rechargeAmount = this.data.msg[index].rechargeAmount
    console.log(index)
    console.log(rechargeAmount)
    this.setData({
      msgIndex: index,
        rechagePrice: _this.data.msg[index].rechargeAmount,
        rechargeAmount: rechargeAmount
    })
    
  
  },
  
  payWay(e){//选择支付方式
    console.log(e)
  },
  submit:function(e){
    console.log(e.detail.formId)
    var formId = e.detail.formId
    this.setData({
      formId: formId
    })
      var formId = this.data.formId
      var rechargeAmount = this.data.rechargeAmount
      console.log( '充值：'+rechargeAmount)
      var token = wx.getStorageSync('token')
      var data = {
        method: 'WeChatPay.rechargeDo',
        token: token,
        rechargeAmount: rechargeAmount,
        formId: formId
      }
      var arr = http.sorts(data)
      data.sign = md5.hex_md5(arr)
      // console.log(arr)
      http._post(data, res => { 
        console.log(res)
        var pay_config = res.data.data.payInfo
        console.log(pay_config)
        // 调用微信支付接口
        wx.requestPayment({
          appId: pay_config.appId,
          timeStamp: pay_config.timeStamp,
          nonceStr: pay_config.nonceStr,
          package: pay_config.package,
          signType: "MD5",
          paySign: pay_config.paySign,    //签名算法
          success: function (res) {
            console.log(res)
            // wx.navigateTo({
            //   url: '/pages/course/soiDetail/soiDetailPay/soiDetailPaySuc/soiDetailPaySuc'
            // })
          },
          fail: function (res) {
            console.log(res)
            wx.showToast({
              title: '用户取消支付',
              icon: 'none',
              duration: 3000,
              success: function () {
                // that.hidePay()
              }
            })
          }
        }) //微信支付接口调用完毕
      })
      // wx.navigateTo({
      //   url: '/pages/myinfo/myMoney/recharge/hundred/hundred?',
      // })
    },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
        let data={
            method:'Wallet.recharge',
            token:wx.getStorageSync('token')
        }
        let arr = http.sorts(data)
        data.sign = md5.hex_md5(arr)
        http._post(data,res=>{
            console.log(res)
            this.setData({
                msg:res.data.data.amountList,
                rechargeAmount: res.data.data.amountList[0].rechargeAmount
            })
        })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})